﻿using PetkaWebDav.Server.Framework.BaseClasses;
using PetkaWebDav.Server.Framework.Classes;
using PetkaWebDav.Server.Framework.Enums;
using PetkaWebDav.Server.Framework.Resources;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudStorageLight.Core.WebDav.Handlers
{
    public class DevPropFind : DavPropFindBase
    {
        protected override async Task OnProcessDavRequest(EventArgs e)
        {
            Debug.WriteLine(this.RequestXml.OuterXml);

            //Check to see if the original resource exists
            BlobItem _fileInfo = await RequestWrapper.DownloadBlobAttributes(this.GetRelativeRequestPath());
            if (_fileInfo == null)
                base.AbortRequest(ServerResponseCode.NotFound);
            else
            {

                DavFile _davFile = new DavFile(_fileInfo.Title, _fileInfo.Path);
                _davFile.CreationDate = AzureUtil.ConvertFromDateTimeOffset(_fileInfo.UploadDate);
                _davFile.LastModified = AzureUtil.ConvertFromDateTimeOffset(_fileInfo.UploadDate);
                _davFile.CustomProperties.Add(new DavProperty("modifiedby", _fileInfo.Metadata["LockOwner"], "urn:schemas-microsoft-com:office:office"));

                //Check to see if there are any locks on the resource
                DavLockProperty _lockInfo = await RequestWrapper.GetLockInfoAsync(_fileInfo.Path);
                if (_lockInfo != null)
                    _davFile.ActiveLocks.Add(_lockInfo);

                ////Check to see if there are any custom properties on the resource
                //DavPropertyCollection _customProperties = RequestWrapper.GetCustomPropertyInfo(_fileInfo.Name);
                //if (_customProperties != null)
                //    _davFile.CustomProperties.Copy(_customProperties);

                _davFile.SupportsExclusiveLock = true;
                _davFile.SupportsSharedLock = true;
                _davFile.ContentLength = (int)_fileInfo.Length;

                base.Resource = _davFile;

            }
        }

    }
}
